package com.lin.zoo.view;

import com.lin.zoo.controller.ActivityController;
import com.lin.zoo.po.Activity;
import com.lin.zoo.util.DataBase;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class ParkVisitorPage {
    private JFrame frame=new JFrame("查看活动与评论");
    private JButton todayButton,parkButton,commentButton;
    private JTable jTable;
    private DefaultTableModel mol;
    private JScrollPane scrollPane;
    private JPanel panel;
    private Vector<String> rowVector;
    private Vector<Vector<String>> dataVector;
    private Connection con;
    private PreparedStatement state;
    private ResultSet rs;
    private ActivityController activityController=new ActivityController();

    public ActivityController getActivityController() {
        return activityController;
    }

    public void setActivityController(ActivityController activityController) {
        this.activityController = activityController;
    }

    public JButton getTodayButton() {
        return todayButton;
    }

    public void setTodayButton(JButton todayButton) {
        this.todayButton = todayButton;
    }

    public JButton getParkButton() {
        return parkButton;
    }

    public void setParkButton(JButton parkButton) {
        this.parkButton = parkButton;
    }

    public JButton getCommentButton() {
        return commentButton;
    }

    public void setCommentButton(JButton commentButton) {
        this.commentButton = commentButton;
    }

    public void createAndShowGUI() throws SQLException{
        setActivityController(new ActivityController());
        frame.setVisible(true);
        frame.setSize(1000,400);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // 显示窗口
        frame.setResizable(false);
        //固定窗口大小不被修改
        frame.setLocationRelativeTo(null);
        //窗口处于中央
        rowVector= new Vector<>();
        rowVector.add("id");
        rowVector.add("园区名字");
        rowVector.add("园区类型");
        rowVector.add("园区介绍");
        rowVector.add("园区面积");
        rowVector.add("园区地址");
        rowVector.add("开园时间");
        rowVector.add("闭园时间");
        rowVector.add("是否开园");
        mol = new DefaultTableModel();
        try {
            con =DataBase.getCon();
            state = con.prepareStatement("select * from park");
            rs=state.executeQuery();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }//直接在view中调用了数据库
        setData(rs);//将rs返回的数据输入到dataVector，再进一步输入到mol
        DataBase.close(state, con);
        jTable=new JTable(mol);
        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scrollPane=new JScrollPane(jTable);

        TableColumn introductionColumn = jTable.getColumnModel().getColumn(3);
        introductionColumn.setPreferredWidth(800);//修改介绍一列的行距

        TableColumn idColumn = jTable.getColumnModel().getColumn(0);
        idColumn.setPreferredWidth(20);//修改id一行的行距

        jTable.setRowHeight(50);
        frame.add(scrollPane,BorderLayout.CENTER);//添加滚轮
        init();

        frame.add(panel,BorderLayout.SOUTH);
        //查看园区活动
        getParkButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int seletedRow=jTable.getSelectedRow();
                seletedRow=jTable.convertRowIndexToModel(seletedRow);
                Object id=jTable.getValueAt(seletedRow+0,0);
                ActivityParkPage parkPage=new ActivityParkPage();
                try {
                    parkPage.createAndShowGUI(id);
                } catch (SQLException ex) {
                    System.out.println("界面打开失败" + this.getClass().getName());
                }
            }
        });
        //查看今日活动
        getTodayButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Date now =getdate();
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");//使用固定的格式输入
                try{
                    con =DataBase.getCon();
                    state = con.prepareStatement("select * from activity");
                    rs=state.executeQuery();
                    List<Activity> list = new ArrayList<>();
                    JFrame jFrame=new JFrame("今日活动");
                    JTextArea jta=new JTextArea("");
                    jFrame.setLayout(null);
                    JScrollPane jsp=new JScrollPane(jta);
                    jsp.setBounds(10,10,980,470);
                    jFrame.add(jsp);
                    jFrame.setSize(1000,500);
                    jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // 显示窗口
                    jFrame.setResizable(false);
                    //固定窗口大小不被修改
                    jFrame.setLocationRelativeTo(null);
                    //窗口处于中央
                    while(rs.next()){
                        try {
                            Date starttime=dateFormat.parse(rs.getString("starttime"));
                            Date endtime=dateFormat.parse(rs.getString("endtime"));
                            if(now.after(starttime) && now.before(endtime)){
                                list.add(new Activity(rs.getString("introduce"),rs.getString("starttime"),rs.getString("endtime")));
                            }
                        } catch (ParseException ex) {
                            System.out.println("数据处理出错" + this.getClass().getName());
                        }
                    }
                    jta.setText(list.toString().replace(",", ""));
                    jta.setFont(new Font("楷书",Font.BOLD,16));
                    jFrame.setVisible(true);

                }catch (ClassNotFoundException | SQLException ex) {
                    System.out.println("数据读取错误" + this.getClass().getName());
                }
            }
        });
        //查看目标园区的评论
        getCommentButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int seletedRow=jTable.getSelectedRow();
                seletedRow=jTable.convertRowIndexToModel(seletedRow);
                Object parkId=jTable.getValueAt(seletedRow+0,0);
                CommentPage commentPage=new CommentPage();
                try {
                    commentPage.createAndShowGUI(parkId);
                } catch (SQLException ex) {
                    System.out.println("数据读取错误" + this.getClass().getName());
                }
            }
        });

    }
   //获取时间
    private Date getdate(){
        return new Date();
    }

    private void init(){
        panel=new JPanel();
        todayButton=new JButton("查看今日活动");
        parkButton=new JButton("查看园区所有活动");
        commentButton=new JButton("查看园区评论");//设置按钮

        panel.setLayout(new GridLayout(1,1));

        panel.add(todayButton);
        panel.add(parkButton);
        panel.add(commentButton);//添加按钮
    }
    //数据读取
    private void setData(ResultSet rs) throws SQLException {
        dataVector = new Vector<>();
        while(rs.next()) {
            Vector<String> vector = new Vector<>();
            for (int i = 1; i <= 9; i++)//9代表列数
                vector.add(rs.getString(i));//添加数据库数据到vector
            dataVector.add(vector);//添加vector数据到dataVector
        }
        mol.setDataVector(dataVector,rowVector);//最后将数据输入至mol
    }

}
